﻿using CallCenter.Data.Base;
using CallCenter.Data.Models;
using CallCenter.Data.Utility;
using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace CallCenter.Data.BizUtilities
{
   public class VendorUtility: BaseUtility
    {

        public VendorUtility(DataManager manager)
            : base(manager)
        { }

        public ReturnMessage<ProductGridViewModel> InsertToVendorProduct(ProductGridViewModel model)
        {
            var returnMessage = new ReturnMessage<ProductGridViewModel>();
            if (model.product.Length == 0)
            {
                returnMessage.Message = "Product Must have a name";
            }

        
            if (String.IsNullOrEmpty(model.description))
                model.description = "";
            model.statusId = 2;

            var id = this.Insert(model.product, model.description, model.companyvendorid, model.statusId);


            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;

                returnMessage.Message = "Recordhas been  inserted to database";
            }

            return returnMessage;
        }

        public ReturnMessage<ProductGridViewModel> UpdateToVendorroduct(ProductGridViewModel model)
        {
            var returnMessage = new ReturnMessage<ProductGridViewModel>();
            if (model.product.Length == 0)
            {
                returnMessage.Message = "Product Must have a name";
            }


           
            if (String.IsNullOrEmpty(model.description))
                model.description = "";
            model.statusId = 2;
            var id = this.Update(model.id,model.product,model.description,model.companyvendorid,model.statusId);


            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
                returnMessage.Message = "Record  has been Updated to database";
            }

            return returnMessage;
        }

        public List<VendorGridVM> GetAllVendorCompanies()
        {
            string sql = "select ct.id, c.id as companyId, ct.description, c.name as companyname from ddlContactTypes ct inner join ddlCompany c on c.companyTypeId = ct.id where ct.id = 2";

            return this.Factory.ExecuteObjects<VendorGridVM>(sql);

        }


        public List<VendorGridVM> GetAllVendorComProducts( int companyId) 
        {
            string sql = "Select p.*, p.description" +
                               "  from ddlProducts p" +
                                                    
                             " WHERE p.companyId = " + companyId +
                                 "and p.id <>5";



            return this.Factory.ExecuteObjects<VendorGridVM>(sql);
        }


        public int Insert(string product, string description, int companyvendorid,int status) //insert data to a contact table
        {
            int nRtn = -1;
            string sql = "INSERT INTO ddlProducts (product,  description, companyId, status)" +
                                 " Values(@product, @description, @companyId , @status) SELECT @pResult =@@IDENTITY";
            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@product", SqlDbType.NChar, 50, product);
              
                this.Factory.AddParameter(cmd, "@description", SqlDbType.VarChar, 50, description);
                this.Factory.AddParameter(cmd, "@companyId", SqlDbType.VarChar, 50, companyvendorid);
                this.Factory.AddParameter(cmd, "@status", SqlDbType.Int, 50, status);





                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }


            return nRtn; //retruning the data stored in the table

        }

        public int Update(int id,string product, string description, int companyvendorid, int status)
        {
            int nRtn = -1;
            string sql = "Update ddlProducts SET product=@product, status=@status, description=@description," +
                                         
                                            " companyId=@companyId WHERE id =@id";




            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@product", SqlDbType.NVarChar, 10, product);
             
                this.Factory.AddParameter(cmd, "@companyId", SqlDbType.Int, 10, companyvendorid);
                this.Factory.AddParameter(cmd, "@status", SqlDbType.Int, 10, status);
                this.Factory.AddParameter(cmd, "@description", SqlDbType.VarChar, 50, description);
               
                this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
                this.Factory.ExecuteNonQuery(cmd);

                nRtn = id;
                return nRtn; //retruning the data stored in the table
            }
        }


    }
}
